--[[
LuCI - Lua Configuration Interface

Copyright 2014 Steven Barth <emongxx@gmail.com>
Blog http://blog.emong.me

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--


m = Map("kxsw", translate("科学上网"),
	translate("通过shadowsocks和redsocks2来实现的科学上网。"))

function m.on_commit(self)
        luci.sys.exec("/etc/init.d/kxsw start")                                                   
end 

s = m:section(TypedSection, "shadowsocks", translate("Shadowsocks"),translate("轻量级的socks代理工具。"))
s.addremove = true

enable = s:option(Flag, "enable", translate("启用"))
enable.default = 1
enable.rmempty = false

startlocal = s:option(Flag, "startlocal", translate("启动ss-local"))
startlocal.default = 0
startlocal.rmempty = false

local_port = s:option(Value, "local_port", translate("ss-local监听端口"),translate("ss-local 的监听端口，请保证此端口的唯一性！"))
local_port.default = 1080
local_port.rmempty = false

startredir = s:option(Flag, "startredir", translate("启动ss-redir"))
startredir.default = 1
startredir.rmempty = false

redir_port = s:option(Value, "redir_port", translate("ss-redir监听端口"),translate("ss-redir 的监听端口，请保证此端口的唯一性！"))
redir_port.rmempty = false
redir_port.default = 7090

dports = s:option(Value, "dports", translate("走代理的tcp端口"),translate("支持多个端口，如80,443,2000:3000，留空则代理tcp协议的所有端口。"))
dports.rmempty = true

server = s:option(Value, "server", translate("服务器IP地址"),translate("直接使用IP地址而不要使用域名，这样效果较好。"))
server.default = "1.2.3.4"

server_port = s:option(Value, "server_port", translate("服务器端口"))
server_port.default = 8388

password = s:option(Value, "password", translate("密码"))
password.rmempty = false
password.password = true

method = s:option(ListValue, "method", translate("加密方式"))
method:value("table")
method:value("rc4")
method:value("aes-128-cfb")
method:value("aes-192-cfb")
method:value("aes-256-cfb")
method:value("bf-cfb")
method:value("camellia-128-cfb")
method:value("camellia-192-cfb")
method:value("camellia-256-cfb")
method:value("cast5-cfb")
method:value("des-cfb")
method:value("rc2-cfb")
method:value("seed-cfb")
method.default = "aes-128-cfb"

timeout = s:option(Value, "timeout", translate("超时"),translate("单位:S。"))
timeout.default = 60

ipsetreverse = s:option(Flag, "ipsetreverse",translate("代理非列表内的地址"),translate("勾选后会对列表之外的地址进行代理。程序会自动排除私网地址和组播地址。"))
ipsetreverse.default = false
ipsetreverse.rmempty = false

ipset = s:option(Value,"_ipset","代理ip列表")
ipset.template = "cbi/tvalue"
ipset.rows = 20
function ipset.cfgvalue(self, section)
	return nixio.fs.readfile("/etc/kxsw/"..tostring(section))
end
function ipset.write(self, section, value)
	value = value:gsub("\r\n?", "\n")
	nixio.fs.writefile("/tmp/etc/kxsw/"..tostring(section), value)
	luci.sys.exec("md5sum /tmp/etc/kxsw/"..tostring(section).." /etc/kxsw/"..tostring(section).." |awk '{print $1}' |tr '\n' ' ' |awk '{if($1!=$2)exit 0;else exit 1}' && cp /tmp/etc/kxsw/"..tostring(section).." /etc/kxsw/"..tostring(section)..";rm /tmp/etc/kxsw/"..tostring(section))
end

return m
